<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>图书管理系统 - 登录</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            font-family: 'Microsoft YaHei', sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
        }
        
        .login-container {
            background: white;
            padding: 40px;
            border-radius: 10px;
            box-shadow: 0 15px 35px rgba(0, 0, 0, 0.1);
            width: 400px;
        }
        
        .login-header {
            text-align: center;
            margin-bottom: 30px;
        }
        
        .login-header h1 {
            color: #333;
            font-size: 24px;
            margin-bottom: 10px;
        }
        
        .login-header p {
            color: #666;
            font-size: 14px;
        }
        
        .form-group {
            margin-bottom: 20px;
        }
        
        .form-group label {
            display: block;
            margin-bottom: 5px;
            color: #333;
            font-weight: 500;
        }
        
        .form-group input {
            width: 100%;
            padding: 12px;
            border: 1px solid #ddd;
            border-radius: 5px;
            font-size: 14px;
            transition: border-color 0.3s;
        }
        
        .form-group input:focus {
            outline: none;
            border-color: #667eea;
        }
        
        .captcha-group {
            display: flex;
            gap: 10px;
        }
        
        .captcha-group input {
            flex: 1;
        }
        
        .captcha-img {
            width: 120px;
            height: 40px;
            border: 1px solid #ddd;
            border-radius: 5px;
            cursor: pointer;
        }
        
        .login-btn {
            width: 100%;
            padding: 12px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            border: none;
            border-radius: 5px;
            font-size: 16px;
            cursor: pointer;
            transition: transform 0.2s;
        }
        
        .login-btn:hover {
            transform: translateY(-2px);
        }
        
        .login-btn:disabled {
            opacity: 0.6;
            cursor: not-allowed;
            transform: none;
        }
        
        .error-message {
            color: #e74c3c;
            font-size: 14px;
            margin-top: 10px;
            text-align: center;
            display: none;
        }
    </style>
</head>
<body>
    <div class="login-container">
        <div class="login-header">
            <h1>图书管理系统</h1>
            <p>请登录您的账户</p>
        </div>
        
        <form id="loginForm">
            <div class="form-group">
                <label for="username">用户名</label>
                <input type="text" id="username" name="username" required>
            </div>
            
            <div class="form-group">
                <label for="password">密码</label>
                <input type="password" id="password" name="password" required>
            </div>
            
            <div class="form-group">
                <label for="captcha">验证码</label>
                <div class="captcha-group">
                    <input type="text" id="captcha" name="captcha" required>
                    <img id="captchaImg" class="captcha-img" alt="验证码">
                </div>
            </div>
            
            <button type="submit" class="login-btn" id="loginBtn">登录</button>
        </form>
        
        <div class="error-message" id="errorMessage"></div>
    </div>

    <script>
        let captchaKey = '';
        
        // 页面加载时获取验证码
        window.onload = function() {
            refreshCaptcha();
        };
        
        // 刷新验证码
        function refreshCaptcha() {
            fetch('/api/auth/captcha')
                .then(response => {
                    captchaKey = response.headers.get('Captcha-Key');
                    return response.blob();
                })
                .then(blob => {
                    const url = URL.createObjectURL(blob);
                    document.getElementById('captchaImg').src = url;
                })
                .catch(error => {
                    console.error('获取验证码失败:', error);
                    showError('获取验证码失败');
                });
        }
        
        // 点击验证码图片刷新
        document.getElementById('captchaImg').addEventListener('click', refreshCaptcha);
        
        // 登录表单提交
        document.getElementById('loginForm').addEventListener('submit', function(e) {
            e.preventDefault();
            
            const username = document.getElementById('username').value;
            const password = document.getElementById('password').value;
            const captcha = document.getElementById('captcha').value;
            
            if (!username || !password || !captcha) {
                showError('请填写完整信息');
                return;
            }
            
            const loginBtn = document.getElementById('loginBtn');
            loginBtn.disabled = true;
            loginBtn.textContent = '登录中...';
            
            fetch('/api/auth/login', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify({
                    username: username,
                    password: password,
                    captcha: captcha,
                    captchaKey: captchaKey
                })
            })
            .then(response => response.json())
            .then(data => {
                if (data.code === 200) {
                    // 登录成功，保存token
                    sessionStorage.setItem('token', data.data.token);
                    sessionStorage.setItem('username', data.data.username);
                    
                    // 跳转到图书管理页面
                    window.location.href = '/book.html';
                } else {
                    showError(data.message);
                    refreshCaptcha();
                }
            })
            .catch(error => {
                console.error('登录失败:', error);
                showError('登录失败，请重试');
                refreshCaptcha();
            })
            .finally(() => {
                loginBtn.disabled = false;
                loginBtn.textContent = '登录';
            });
        });
        
        // 显示错误信息
        function showError(message) {
            const errorElement = document.getElementById('errorMessage');
            errorElement.textContent = message;
            errorElement.style.display = 'block';
            
            setTimeout(() => {
                errorElement.style.display = 'none';
            }, 3000);
        }
    </script>
</body>
</html> 